{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用tf-idf对词向量加权的形式导致得到的“句向量”精度不高，如用bert等更强大的句向量，效果应该会更好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from utils import tokenize, load_curpus\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 0.512 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "train_data = load_curpus(\"weibo2018/train.txt\")\n",
    "test_data = load_curpus(\"weibo2018/test.txt\")\n",
    "train_df = pd.DataFrame(train_data, columns=[\"content\", \"sentiment\"])\n",
    "test_df = pd.DataFrame(test_data, columns=[\"content\", \"sentiment\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 加载停用词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "stopwords = []\n",
    "with open(\"stopwords.txt\", \"r\", encoding=\"utf8\") as f:\n",
    "    for w in f:\n",
    "        stopwords.append(w.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### TfIdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "data_str = [\" \".join(content) for content, sentiment in train_data] + \\\n",
    "            [\" \".join(content) for content, sentiment in test_data]\n",
    "tfidf = TfidfVectorizer(token_pattern='\\[?\\w+\\]?', stop_words=stopwords)\n",
    "tfidf_fit = tfidf.fit_transform(data_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 加载之前训练好的FastText模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from gensim.models import FastText\n",
    "model = FastText.load(\"model/model_100.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 最多只保留Tf-Idf最高的前多少个词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "key_words = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 用每个词的Tfidf作为权重, 对FastText词向量进行加权, 得到表征每个句子的向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/albert/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: DeprecationWarning: Call to deprecated `__contains__` (Method will be removed in 4.0.0, use self.wv.__contains__() instead).\n",
      "  import sys\n",
      "/home/albert/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:8: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "X_train, y_train = [], []\n",
    "for content, sentiment in train_data:\n",
    "    X, y = [], sentiment\n",
    "    X_tfidf = tfidf.transform([\" \".join(content)]).toarray()\n",
    "    keywords_index = np.argsort(-X_tfidf)[0, :key_words]\n",
    "    for w in content:\n",
    "        if w in model and w in tfidf.vocabulary_ and tfidf.vocabulary_[w] in keywords_index:\n",
    "            X.append(np.expand_dims(model[w], 0) * X_tfidf[0, tfidf.vocabulary_[w]])\n",
    "    if X:\n",
    "        X = np.concatenate(X)\n",
    "        X = np.expand_dims(np.mean(X, axis=0), 0)\n",
    "        X_train.append(X)\n",
    "        y_train.append(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/albert/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: DeprecationWarning: Call to deprecated `__contains__` (Method will be removed in 4.0.0, use self.wv.__contains__() instead).\n",
      "  import sys\n",
      "/home/albert/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:8: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "X_test, y_test = [], []\n",
    "for content, sentiment in test_data:\n",
    "    X, y = [], sentiment\n",
    "    X_tfidf = tfidf.transform([\" \".join(content)]).toarray()\n",
    "    keywords_index = np.argsort(-X_tfidf)[0, :key_words]\n",
    "    for w in content:\n",
    "        if w in model and w in tfidf.vocabulary_ and tfidf.vocabulary_[w] in keywords_index:\n",
    "            X.append(np.expand_dims(model[w], 0) * X_tfidf[0, tfidf.vocabulary_[w]])\n",
    "    if X:\n",
    "        X = np.concatenate(X)\n",
    "        X = np.expand_dims(np.mean(X, axis=0), 0)\n",
    "        X_test.append(X)\n",
    "        y_test.append(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 神经网络\n",
    "两层全连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dengxiuqi/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "batch_size = 1000\n",
    "lr = 1e-3\n",
    "X = tf.placeholder(shape=(batch_size, 100), dtype=tf.float32, name=\"X\")\n",
    "y = tf.placeholder(shape=(batch_size, 1), dtype=np.float32, name=\"y\")\n",
    "with tf.variable_scope(\"fcn\", reuse=tf.AUTO_REUSE):\n",
    "    W1 = tf.get_variable(\"W1\", shape=(100, 50))\n",
    "    b1 = tf.get_variable(\"b1\", shape=(50,))\n",
    "    W2 = tf.get_variable(\"W2\", shape=(50, 1))\n",
    "    b2 = tf.get_variable(\"b2\", shape=(1,))\n",
    "    fcn1 = tf.nn.xw_plus_b(X, W1, b1)\n",
    "    logists = tf.nn.xw_plus_b(fcn1, W2, b2)\n",
    "    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logists, labels=y))    # 交叉熵损失\n",
    "    op = tf.train.AdamOptimizer(lr).minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)\n",
    "config = tf.ConfigProto(gpu_options=gpu_options)\n",
    "sess = tf.Session(config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step: 0  loss: 0.9794157\n",
      "step: 100  loss: 0.50017077\n",
      "step: 200  loss: 0.48402408\n",
      "step: 300  loss: 0.49401006\n",
      "step: 400  loss: 0.48483914\n",
      "step: 500  loss: 0.47343573\n",
      "step: 600  loss: 0.47297615\n",
      "step: 700  loss: 0.46698982\n",
      "step: 800  loss: 0.46584013\n",
      "step: 900  loss: 0.4559707\n",
      "step: 1000  loss: 0.4664424\n"
     ]
    }
   ],
   "source": [
    "total_step = 1001\n",
    "step = 0\n",
    "cursor = 0\n",
    "sess.run(tf.global_variables_initializer())\n",
    "saver = tf.train.Saver(max_to_keep=1)\n",
    "while step < total_step:\n",
    "    _X, _y = X_train[cursor: cursor + batch_size], y_train[cursor: cursor + batch_size]\n",
    "    cursor += batch_size\n",
    "    if len(_X) < batch_size:\n",
    "        cursor = batch_size - len(_X)\n",
    "        _X += X_train[: cursor]\n",
    "        _y += y_train[: cursor]\n",
    "    _X = np.concatenate(_X)\n",
    "    _y = np.reshape(np.array(_y, dtype=np.float32), (batch_size, 1))\n",
    "    _, l = sess.run([op, loss], feed_dict={X: _X, y: _y})\n",
    "    if step % 100 == 0:\n",
    "        print(\"step:\", step, \" loss:\", l)\n",
    "        saver.save(sess,'model/nn/model', global_step=step)\n",
    "    step += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "_X = np.concatenate(X_test + [np.zeros_like(X_test[0])] * (batch_size - len(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "result = sess.run(tf.nn.sigmoid(logists), feed_dict={X: _X})\n",
    "prediction = []\n",
    "for i in result[:len(X_test)]:\n",
    "    if i > 0.5:\n",
    "        prediction.append(1)\n",
    "    else:\n",
    "        prediction.append(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 效果测评\n",
    "主要还是因为用tf-idf对词向量加权的形式导致得到的“句向量”精度不高，如用bert等更强大的句向量，效果应该会更好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.68      0.74      0.71       155\n",
      "          1       0.88      0.84      0.86       344\n",
      "\n",
      "avg / total       0.82      0.81      0.81       499\n",
      "\n",
      "准确率: 0.8096192384769539\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "print(metrics.classification_report(y_test, prediction))\n",
    "print(\"准确率:\", metrics.accuracy_score(y_test, prediction))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
